﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Async_01 {
    class Program {
        static void Main (string[] args) {
            #region 测试
            while (true) {
                if (Console.KeyAvailable) {
                    ConsoleKeyInfo keyInfo = Console.ReadKey (true);
                    switch (keyInfo.Key) {
                        case ConsoleKey.A:
                            Console.WriteLine ("按下键盘" + keyInfo.Key);
                            ASyncSmaple_01 async_01 = new ASyncSmaple_01 ();
                            //调用分两种形式，一种是阻塞式的，一种是非阻塞的
                            //当async_01.StartAsync 的返回值为Task,调用方式为async_01.StartAsync ().wait()为阻塞式的
                            //当async_01.StartAsync ()的返回值为void时，调用方式为async_01.StartAsync ()非阻塞式的
                            //如果要在主线程中那到计算的结果
                            var  result=async_01.StartAsync ().Result;

                            Console.WriteLine("主线程中执行打印:Result="+result.Item1+":"+result.Item2);


                            break;
                        case ConsoleKey.S:
                            Console.WriteLine ("按下键盘" + keyInfo.Key);
                            ASyncSmaple_02 async_02 = new ASyncSmaple_02 ();
                            async_02.AddAsync ();
                            break;
                        case ConsoleKey.D:
                            Console.WriteLine ("按下键盘" + keyInfo.Key);
                            ASyncSmaple_03 async_03 = new ASyncSmaple_03 ();
                            async_03.GetGuidAsync ().Wait();

                            
                            break;
                        case ConsoleKey.F:
                            JsonTest jsontest = new JsonTest ();
                            string jsonStr="{\"name\":\"Superm\",\"ExpiryDate\":\"2019-12-28T00:00:00\",\"Price\":3.9,\"Sizes\":[\"SunME\",\"Superman\",\"dayubab\"]}";
                            //{"name":"Apple","ExpiryDate":"2019-12-28T00:00:00","Price":3.9,"Sizes":["Small","Medium","Large"]}
                           // JsonConvert.DeserializeObject({"name":"Apple","ExpiryDate":"2019-12-28T00:00:00","Price":3.9,"Sizes":["Small","Medium","Large"]})

                           Product product=JsonConvert.DeserializeObject<Product>(jsonStr);

                           Console.WriteLine("{0},{1},{2},{3}",product.name,product.ExpiryDate,product.Price,product.Sizes[0]);
                       break;
                    }
                }
            }
            #endregion
        }
        //static int Add(int a,int b){
        //    return a+b;
        //}
        //static async void AddAsync(int a,int b){
        //    int val=await Task.Run(()=>Add(a,b));
        //    Console.WriteLine("结果：{0}",val);
        //}
    }
}